﻿//站点配置
var domainInfo = {
    host: document.domain,
    //小程序后台域名
    weappDomain: "http://weadmin.711688.net.cn",
    //订阅宝后台域名
    shtDomain: "http://wxpassport.711688.net.cn",
    //API域名
    apiDomain: "http://api.711688.com.cn"
};

if (document.location.href.indexOf("127.0.0.1") > -1) {
    domainInfo = {
        host: document.domain,
        //小程序后台域名
        weappDomain: "http://127.0.0.1:9022",
        //订阅宝后台域名
        shtDomain: "http://127.0.0.1:9011",
        //API域名
        apiDomain: "http://127.0.0.1/api"
    };
}

//url参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = decodeURI(window.location.search).substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}

//返回字符串中的值，字符串是url格式
function getUrlParam(urlString, name) {
    urlString = "&" + urlString;
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = decodeURI(urlString).substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return "";
}

//获取cookie
function getCookie(name) {
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]); return null;
}

function getCookies(name, key) {
    var values = "&" + getCookie(name);
    if (values != null) {
        var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)", "i");
        var r = decodeURI(values).substr(1).match(reg);
        if (r != null)
            return unescape(r[2]);
    }
    return "";
}

//链接跳转
function toUrl(url) {
    window.location.href = url;
}

//登录跳转
function toLogin() {
    window.location.href = "http://www.711688.net.cn";
}

//是否登录
function isLogin() {
    var loginSign = getUserId();

    if (loginSign == '')
        return false;
    return true;
}
function getUserId() {
    return getCookies("shtusers", "UserID") != "" ? getCookies("shtusers", "UserID") : getCookies("users", "UserID");
}

//iframe高度自适应
function iFrameHeight(id) {
    var _iframe = document.getElementById(id);
    if (_iframe == null) return;

    try {
        var iframe = document.getElementById(id);
        var bHeight = iframe.contentWindow.document.body.scrollHeight;
        var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
        var height = bHeight > 500 ? Math.max(bHeight, dHeight) : 500;  //最低500高度
        iframe.height = height;
    } catch (err) {
        console.log("iframe高度设置意外终止");
    }
}

function loadIframe(default_url) {
    var path = getQueryString("path");
    if (path == null || path == '') {
        if (default_url == null)
            return;
        path = default_url;
    }
    setIframeUrl(path);
}

function setIframeUrl(url) {
    var iframe = document.getElementById("frameRight");
    iframe.height = 500;    //初始化高度
    if (url.indexOf("?") > -1 || url.indexOf("&") > -1)
        iframe.src = url + "&_=" + Math.random().toString().substring(1);
    else
        iframe.src = url + "#_=" + Math.random().toString().substring(1);

    setHelpLink();  //设置帮助文档
}


//对菜单点击，执行跳转页面操作
function redirectFrame(obj) {
    var className = obj.attr("class");
    var path = obj.attr("data-url");
    var cerrentUrl = document.location.href;

    if (className.indexOf("redirect") == -1)	//只有 class="menu-redirect" 才执行跳转
        return;

    //当前选中的导航
    var navIndex = $(".content-title .on").attr("data-index") == null ? "1" : $(".content-title .on").attr("data-index");
    var menu_ul = obj.parent().parent().parent().attr("menu-ul");   //当前菜单列表
    var menu_li = obj.parent().attr("menu-li");                     //当前菜单项
    menu_ul == null ? "" : menu_ul;
    menu_li == null ? "" : menu_li;

    var hash_path = "#nav=" + navIndex + "&menu_ul=" + menu_ul + "&menu_li=" + menu_li;

    //商汇通后台
    if (cerrentUrl.indexOf(domainInfo.shtDomain) > -1) {
        console.log("## is sht");
        if (path.indexOf("storeadmin") > -1)
            toUrl(domainInfo.weappDomain + "/storeadmin/?path=" + encodeURIComponent(path) + hash_path);    //跳小程序后台
        else
            setIframeUrl(path); //跳商汇通后台
    }

    //小程序后台
    if (cerrentUrl.indexOf(domainInfo.weappDomain) > -1) {
        console.log("## is weapp");
        if (path.indexOf("storeadmin") == -1)
            toUrl(domainInfo.shtDomain + "/wxpassport/admin_index.aspx?path=" + encodeURIComponent(path) + hash_path); //跳商汇通后台
        else
            setIframeUrl(path); //跳小程序后台
    }
}

//加载菜单
function loadMenu() {
    if (isWeapp)
        $(".gtype_15").removeClass("hide");
    if (isDis)
        $(".gtype_18").removeClass("hide");
    if (isAgent)
        $(".nav_agent").removeClass("hide");
    if (isProject) {
        $(".gtype_15").removeClass("hide");
        $(".gtype_20").removeClass("hide");
    }
    if (is_Q3_Project) {
        $(".gtype_Q3").removeClass("hide");
    }
}

//加载代理菜单
function loadAgentMenu() {
    var kind = getCookies("ua", "kind");
    if (kind == 0)
        $(".agent-kind").removeClass("hide");

    if (isGrant(15) || isGrant(16) || isGrant(16) || isGrant(19)) {
        isAgent = true;
        $(".gtype_15").removeClass("hide");
    }
    if (isGrant(9)) {
        isAgent = true;
        $(".gtype_9").removeClass("hide");
    }
    if (isGrant(11)) {
        isAgent = true;
        $(".gtype_11").removeClass("hide");
    }
    if (isGrant(13)) {
        isAgent = true;
        $(".gtype_13").removeClass("hide");
    }
}

function isGrant(key) {
    return getCookies("ua", key) != "" ? true : false;
}

//根据URL的锚点，选中导航、菜单
function setMenu() {
    var hash = document.location.hash == '' ? "" : document.location.hash.substring(1);
    var navIndex = getUrlParam(hash, "nav") == "" ? "1" : getUrlParam(hash, "nav");
    var menu = getUrlParam(hash, "menu");

    var obj = null, first = null;
    $(".content-title-item").each(function () {
        if ($(this).attr("class").indexOf("hide") == -1) {
            if ($(this).attr("data-index") == navIndex) {
                $(this).click();
                obj = $(this);
            }
            if (first == null) first = $(this); //默认选中第一个导航
        }
    });
    if (obj == null && first != null)
        first.click();
}

//首页咨询浮动
function loadFD() {
    var that = $(".seven");
    var windowWidth = document.documentElement.clientWidth;
    var thatWidth = $(".seven").css("width").replace("px", "");
    var rightWidth = ((windowWidth - 1200) / 2) - thatWidth;
    that.css("right", rightWidth);
    that.css("display", "");
}

//加载版本
function loadGroup() {
    var userid = getUserId();
    ajaxJsonp(domainInfo.apiDomain + "/user.ashx?userid=" + userid + "&method=getusergroup&format=jsonp", "", function (data) {
        if (data.Code != jsonCode.success) {
            toast.show(data.Message);
            return;
        }

        var groupList = data.Data.list;
        if (groupList != null) {
            for (var i = 0; i < groupList.length; i++) {
                var item = groupList[i];
                if (item.GroupType >= 15 && item.GroupType <= 19) {
                    isWeapp = true;   //小程序
                    $(".expiration").html(item.ExpireTime.substring(0, 10));
                }
                if (item.GroupType == 18 || (item.GroupType == 20 && item.GroupId != 82))
                    isDis = true;     //分销版

                if (item.GroupType == 20) {
                    isWeapp = true;
                    if (item.GroupId == 82)
                        is_Q3_Project = true;   //方案Q3
                    else
                        isProject = true;       //方案Q7
                }
            }
        }

        if (data.Data != null) {
            $(".nickName").html(data.Data.username);
            $(".sitename").html(data.Data.sitename);
            $(".regtime").html(data.Data.regtime.substring(0, 10));
        }
        loadMenu(); //加载菜单
        setMenu();  //选中第一个菜单
    })
}

//加载帮助中心
var listHelp = null;
function loadHelp() {
    ajaxJsonp(domainInfo.apiDomain + "/news/news.ashx?action=gethelplist&format=jsonp&callback=JsonpCallback", "", function (data) {
        if (data.Code != jsonCode.success) {
            toast.show(data.Message);
            return;
        }
        listHelp = data.Data;
        setHelpLink();
    })
}

//设置帮助文档链接
function setHelpLink() {
    var help_url = "http://www.711688.com.cn/help.aspx?columnid=11"; //帮助文档url
    var page_url = $("#frameRight").attr("src");  //当前页面url
    if (listHelp != null) {
        for (var i = 0; i < listHelp.length; i++) {
            var item = listHelp[i];
            if (page_url.indexOf(item["keywords"]) > -1) {
                console.log("匹配帮助文档");
                help_url = "http://www.711688.com.cn/help_item.aspx?newsid=" + item["newsid"] + "&columnid=" + item["columnid"];
                break;
            }
        }
    }
    //绑定链接
    $(".help").attr("href", help_url);
}

$(function () {
    //自适应内容高度
    if (document.getElementById("frameRight") != null)
        setInterval(function () { iFrameHeight("frameRight") }, 500);
});